
angular
    .module('app.service')
    .service('MarkSubjectService', MarkSubjectService);

MarkSubjectService.$inject = ['API','$http'];
function MarkSubjectService(API,$http) {
    var baseUrl = API+'marksubjects';
    this.get = get;
    this.questionStat = questionStat;
    this.levelStat = levelStat;
    this.markers = markers;
    this.markerGroups = markerGroups;
    this.saveMarkerGroups = saveMarkerGroups;
    this.deleteMarkerGroups = deleteMarkerGroups;
    this.goStage = goStage;
    //////////////////
    function get(markSubjectId){
        return $http.get(baseUrl+'/'+markSubjectId);
    }
    function levelStat(markSubjectId,questionId) {
        var url = baseUrl+'/'+markSubjectId+'/stat/levels?questionId='+questionId;
        return $http.get(url);
    }
    function questionStat(markSubjectId) {
        var url = baseUrl+'/'+markSubjectId+'/stat/questions';
        return $http.get(url);
    }

    function markers(markSubjectId){
        var url = baseUrl+'/'+markSubjectId+'/markers';
        return $http.get(url);
    }

    function markerGroups(markSubjectId){
        var url = baseUrl+'/'+markSubjectId+'/markergroups';
        return $http.get(url)
    }

    function saveMarkerGroups(markSubjectId,markerGroup){
        if(markerGroup.id){
            var url = baseUrl+'/'+markSubjectId+'/markergroups/'+markerGroup.id;
            return $http.patch(url,markerGroup);
        }
        else{
            var url = baseUrl+'/'+markSubjectId+'/markergroups';
            return $http.post(url,markerGroup);
        }
    }

    function deleteMarkerGroups(markSubjectId,markerGroup){
        var url = baseUrl+'/'+markSubjectId+'/markergroups/'+markerGroup.id;
        return $http.delete(url,markerGroup);
    }

    function goStage(markSubjectId){
        var url = baseUrl+'/'+markSubjectId;
        return $http.patch(url);
    }
}